package chen.web;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import chen.web.user.Account;

/**
 * 用于存储上下文，在spring中的scope是request, 每次发送http请求都会创建一个下个文.
 * 
 */
public class MyContext {
	
	//当前用户帐号
	private Account account;
	//用户识别码, 每个浏览器都是唯一的. 用来确认用户
	private String uuid;
	//用户ip
	private String ip;

	private Map<String, Object> attributes = null;
	
	public void clear(){
		account = null;
		uuid = null;
		ip = null;
		if(attributes != null){
			attributes.clear();
		}
	}
	
	public Object getAttribute(String name){
		if(attributes != null){
			return attributes.get(name);
		}
		return null;
	}
	
	public void setAttribute(String name, Object object){
		if(attributes == null){
			attributes = new HashMap<String, Object>();
		}
		attributes.put(name, object);
	}
	
	public void removeAttribute(String name){
		if(attributes != null){
			attributes.remove(name);
		}
	}
	
	public Set<String> getAttributeNames(){
		if(attributes != null){
			return attributes.keySet();
		}
		return new HashSet<String>(0);
	}

	void setIp(String ip) {
		this.ip = ip;
	}

	public String getIp() {
		return ip;
	}

	void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public String getUuid() {
		return uuid;
	}

	void setAccount(Account account) {
		this.account = account;
	}

	public Account getAccount() {
		return account;
	}
	
	private final String ThemeAttributeName = "ThemeName";
	public String getTheme(){
		return (String)this.getAttribute(ThemeAttributeName);
	}
	
	public void setTheme(String theme){
		this.setAttribute(ThemeAttributeName, theme);
	}
	
}
